package com.define.ssh.action.userconfig;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.classic.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.StringUtils;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.common.JsonUtil;
import com.define.ssh.model.Compdept;
import com.define.ssh.model.Deptuser;
import com.define.ssh.model.FictitiousUser;
import com.define.ssh.model.GroupinfoRegular;
import com.define.ssh.model.GroupmemberRegular;
import com.define.ssh.model.UserInfo;
import com.define.ssh.model.UserRoamingdata;
import com.define.ssh.service.CompdeptService;
import com.define.ssh.service.DeptuserService;
import com.define.ssh.service.GroupinfoRegularService;
import com.define.ssh.service.GroupmemberRegularService;
import com.define.ssh.service.UserInfoService;
import com.define.ssh.service.UserRoamingdataService;
import com.define.ssh.util.Pager.Order;
import com.define.ssh.util.InterfaceIpData;
import com.define.ssh.util.SystemUtil;
import com.sxit.ecwx.core.utils.Formatu;


/***
 * 
 * @author sc
 *
 */
@ParentPackage("userconfig")
public class GroupManagementAction extends  BaseAdminAction{

	private static final long serialVersionUID = 4L;
	/** 页面传递参数过来*/
	protected int pageSize;
	protected int pageNum;
	
	private String node;
	private Integer pid;
	private Integer deptid;
	
	@Resource(name = "compdeptServiceImpl")
	private CompdeptService compdeptService;
	
	@Resource(name = "userInfoServiceImpl")
	private UserInfoService userInfoService;
	
	@Resource(name = "deptuserServiceImpl")
	private DeptuserService deptuserService;
	
	@Resource(name = "userRoamingdataServiceImpl")
	private UserRoamingdataService userRoamingdataService;
	
	@Resource(name = "groupinfoRegularServiceImpl")
	private  GroupinfoRegularService groupinfoRegularService;
	
	
	@Resource(name = "groupmemberRegularServiceImpl")
	private  GroupmemberRegularService groupmemberRegularService;
	
	private String uname;
	private Integer utype;
	private List  deptList=new ArrayList();
	private String deptids;
	private String groupid;
	private String data;
	private GroupinfoRegular groupinfoRegular;

	public String getGroupid() {
		return groupid;
	}



	public void setGroupid(String groupid) {
		this.groupid = groupid;
	}



	public String index(){
		
		/*
		 DetachedCriteria dt = DetachedCriteria.forClass(Compdept.class);	
			List<Compdept> wyulist=compdeptService.findByCriteria(dt);
		System.out.println("==============="+wyulist.size());*/
		
/*		
		HashMap<String, Object> searchMap = new HashMap<String, Object>();
		pager.setSearchMap(searchMap);
		pager = compdeptService.findPager(pager);*/
		
		return "index";
	}
	

	
	/**
	 * 取部门树数据
	 * @return
	 */
	public String getDeptUserTree(){
	/*	List<Compdept> list=this.compdeptService.getAllList();
		List<Deptuser> du_list=this.deptuserService.getAllList();
		StringBuffer sb=new StringBuffer();
		sb.append("[");
		for(int i=0;i<list.size();i++){
			Compdept dept=list.get(i);
			sb.append("{");
			sb.append("id:"+dept.getId());
			sb.append(",pId:"+dept.getPid());
			sb.append(",name:\""+dept.getDeptname()+"\"");
			if(dept.getPid().equals("0")){
				sb.append(",open:true");
			}
			sb.append("}");
			if(du_list.size()>0){
				sb.append(",");
			}
		}
		for(int i=0;i<du_list.size();i++){
			Deptuser du=du_list.get(i);
			sb.append("{");
			sb.append("id: \"u"+du.getUserid()+"\"");
			sb.append(",pId:"+du.getDeptid());
			sb.append(",name:\""+du.getUserInfo().getName()+"\"");
			sb.append(",icon:\"../resources/admin/css/bgimg/u88.png\"");
			sb.append("}");
			if(i!=du_list.size()-1){
				sb.append(",");
			}
		}
		sb.append("]");
		return ajax(JSONArray.fromObject(sb.toString()).toString());*/
		
		
		 DetachedCriteria dt = DetachedCriteria.forClass(Compdept.class);
		 dt.add(Restrictions.eq("company.id",InterfaceIpData.compid));
		 dt.add(Restrictions.eq("pid",0));
		 dt.add(Restrictions.in("updatetype", new Object[]{1,2}));
		 List<Compdept> list = this.compdeptService.findByCriteria(dt);
		
		 //List<Compdept> list = this.compdeptService.getAllList();
		 StringBuffer sb = new StringBuffer();
		sb.append("[");
		for (int i = 0; i < list.size(); i++) {
			final Compdept dept = list.get(i);
			sb.append("{");
			sb.append("id:" + dept.getId());
			sb.append(",pId:" + dept.getPid());
			sb.append(",name:\"" + dept.getDeptname() + "\"");
			
			 DetachedCriteria dt1 = DetachedCriteria.forClass(Compdept.class);
			 dt1.add(Restrictions.eq("pid",dept.getId()));
			 List<Compdept> list1 = this.compdeptService.findByCriteria(dt1);
			if(list1!=null&&list1.size()>0){
				sb.append(",open:true");
				sb.append(",isParent:true");
			}
			sb.append("}");
			if (i != list.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		return ajax(JSONArray.fromObject(sb.toString()).toString());
	}
	
	
	/**
	 * 取部门树数据
	 * 
	 * @return
	 */
public String getUserTT() {
		
		try {
			String deptid = super.getRequest().getParameter("id");
			DetachedCriteria dt1 = DetachedCriteria.forClass(Compdept.class);
			dt1.add(Restrictions.eq("company.id",InterfaceIpData.compid));
			dt1.add(Restrictions.eq("pid",Integer.valueOf(deptid)));
			dt1.add(Restrictions.ne("updatetype", 3));
			List<Compdept> list = this.compdeptService.findByCriteria(dt1);
			//DetachedCriteria dt = DetachedCriteria.forClass(Deptuser.class);
			//dt.add(Restrictions.eq("deptid",Integer.valueOf(deptid)));
			//List<Deptuser> du_list = this.compdeptService.findByCriteria(dt);
			 
			StringBuffer sb=new StringBuffer();
			sb.append("[");
			for(int i=0;i<list.size();i++){
				Compdept dept=list.get(i);
				sb.append("{");
				sb.append("id:"+dept.getId());
				sb.append(",pId:"+dept.getPid());
				sb.append(",name:\""+dept.getDeptname()+"\"");
				Integer count=compdeptService.count(dept.getId());
				if(count>0){
					sb.append(",open:true");
					sb.append(",isParent:true");
				}
				sb.append("}");
				if(list.size()>0){
					sb.append(",");
				}
			}
			/*
			for(int i=0;i<du_list.size();i++){
				Deptuser du=du_list.get(i);
				sb.append("{");
				sb.append("id: \""+du.getDeptid()+"_"+du.getUserid()+"\"");
				sb.append(",pId:"+du.getDeptid());
				sb.append(",name:\""+du.getUserInfo().getName()+"\"");
				sb.append(",icon:\"../resources/admin/css/bgimg/u88.png\"");
				
				DetachedCriteria dc=DetachedCriteria.forClass(FictitiousUser.class);
				dc.add(Restrictions.eq("fid", fteam.getId()));
				dc.add(Restrictions.eq("userid", Long.valueOf(du.getUserid()+"")));
				List<FictitiousUser> fu_list=this.fictitiousUserService.findByCriteria(dc);
				if(fu_list.size()>0){
					sb.append(",checked : true");
				}	
				sb.append("}");
				if(i!=du_list.size()){
					sb.append(",");
				}
			}
			*/
			sb.append("]");
			return ajax(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public String getUserListByDept() {
		try {
		
			
			String deptidStr = super.getRequest().getParameter("id");
			Integer deptid=null;
			if(StringUtils.hasText(deptidStr)){
				deptid=Integer.valueOf(deptidStr);
			}
			
			pager=this.deptuserService.find(pager, deptid, data);
			
	
			List<Deptuser> du_list = (List<Deptuser>) pager.getResult();
			StringBuffer sb=new StringBuffer();
			
			sb.append(",\"list\":[");
			for(Deptuser du:du_list){
				
					if(sb.length()>10){
						sb.append(",");
					}
					sb.append("{");
					sb.append("\"id\":\""+du.getUserInfo().getId()+"\"");
					sb.append(",\"name\":\""+du.getUserInfo().getName()+"\"");
					sb.append(",\"deptName\":\""+du.getDept().getDeptname()+"\"");
					sb.append(",\"local\":\""+du.getUserInfo().getLocal()+"\"");
					sb.append("}");
			}
			sb.append("]");
			
			StringBuffer sb1=new StringBuffer();
			sb1.append("{");
			sb1.append("\"pageCount\":\""+pager.getPageCount()+"\"");
			sb1.append(",\"pageNumber\":\""+pager.getPageNumber()+"\"");
			sb1.append(sb);
			sb1.append("}");
			return ajax(sb1.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	/**
	 *
	 * @return
	 */
	public String userInfo() {
		
		try {
			
			UserInfo userInfo=userInfoService.get(id);
			return ajax(JsonUtil.beanToJson(userInfo));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		
		try {
		UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
		
		HashMap<String, Object> searchMap = new HashMap<String, Object>();
		if(userinfo!=null){
			// DetachedCriteria dt1 = DetachedCriteria.forClass(GroupinfoRegular.class);	
		
			
			    // szUrDate();
				
				/*	"1""超级管理员"	
				"2" "系统管理员"
				"3""部门管理员"
				"4""操作员"*/
				if(userinfo.getRole()!=null&&(userinfo.getRole().getId()==1||userinfo.getRole().getId()==2)){
			
				}else{
					searchMap.put("userid",userinfo.getId());
					 //dt1.add(Restrictions.eq("userid",userinfo.getId()));
				
				}
				if(StringUtils.hasText(uname)){
					 searchMap.put("groupname",uname);
					// dt1.add(Restrictions.eq("groupname",uname));
				}
				   
		   // searchMap.put("updatetype", 1);	
			pager.setOrder(Order.desc);
			pager.setOrderBy("updateTime");
			pager.setSearchMap(searchMap);
			Object[] type={1,2};
			pager =groupinfoRegularService.findPager(pager,Restrictions.in("updatetype", type));
			
			List<GroupinfoRegular> list=(List<GroupinfoRegular>)pager.getResult();
			if(list!=null&&list.size()>0){
				for (GroupinfoRegular groupinfoRegular : list) {
					 StringBuffer sb=new StringBuffer();
					 DetachedCriteria dt = DetachedCriteria.forClass(GroupmemberRegular.class);	
					 dt.add(Restrictions.eq("groupid",groupinfoRegular.getGroupid()));
					 dt.add(Restrictions.eq("isadmin",1));
					 List<GroupmemberRegular> gulist=compdeptService.findByCriteria(dt);
					 for (GroupmemberRegular groupmemberRegular : gulist) {
						if(groupmemberRegular.getUserInfo()!=null){
						 sb.append(groupmemberRegular.getUserInfo().getName()+"   ");
						}
					}
					 groupinfoRegular.setMainNames(sb.toString()); 
				}
				  pager.setResult(list);
			}
				
				
		}
		


		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}
	
	
	
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String listInfo() {
		
		try {
		UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
		if(userinfo!=null){
			
			
		
			
			  //   szUrDate();
				
				/*	"1""超级管理员"	
				"2" "系统管理员"
				"3""部门管理员"
				"4""操作员"*/
				if(userinfo.getRole()!=null&&(userinfo.getRole().getId()==1||userinfo.getRole().getId()==2)){
			
					
					
			}
				
				 DetachedCriteria dt = DetachedCriteria.forClass(GroupinfoRegular.class);	
				 dt.add(Restrictions.eq("groupid", groupid));
				 List lsit=groupinfoRegularService.findByCriteria(dt);
				 
			
				
				if(lsit!=null&&lsit.size()>0){
					 groupinfoRegular=(GroupinfoRegular)lsit.get(0);		
			}
				
			//String name = super.getRequest().getParameter("name");
			HashMap<String, Object> searchMap = new HashMap<String, Object>();
			searchMap.put("groupid",groupid);
			if(StringUtils.hasText(uname)){
			searchMap.put("name",uname);
			}
			//pager.setSearchMap(searchMap);
			//pager =groupmemberRegularService.findPager(pager);
			
			pager = groupinfoRegularService.findPagerByGroupinfoRegular(pager,searchMap);
				
		}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return "networkProject";
	}
	

	public int getPageSize() {
		return pageSize;
	}


	public String addUser(){
		
	try {
			
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
		
		
		String amdinUserIds = super.getRequest().getParameter("amdinUserIds");
		String userIds = super.getRequest().getParameter("userIds");
		String groupname = super.getRequest().getParameter("groupName");
		String description = super.getRequest().getParameter("groupMs");
		
		//Long time=System.currentTimeMillis()/1000;
		
		GroupinfoRegular ginfo=new GroupinfoRegular();
		ginfo.setGroupid(getmId());
		ginfo.setGroupname(groupname);
		ginfo.setDescription(description);
		ginfo.setCompid(userinfo.getCompid());
		//ginfo.setUsernum(userinfo.getId());
		ginfo.setForbidden(0);
		ginfo.setUpdateTime(new Date());
		ginfo.setUpdatetype(1);//1、新增 2、修改 3、删除
		ginfo.setUserid(userinfo.getId());
		ginfo.setCreaterid(userinfo.getId());
		ginfo.setProclamation(description);
		Date updatetime = groupinfoRegularService.getUpdateTime("groupinfo_regular");
		Integer count=0;
		Integer usercount=userIds.split(",").length;
		Integer admincount=amdinUserIds.split(",").length;
		if(usercount!=0){
			count=count+usercount-1;
		}
		if(admincount!=0){
			count=count+admincount-1;
		}
		ginfo.setUsernum(Long.valueOf(count));
		groupinfoRegularService.save(ginfo);
		List<GroupmemberRegular> list=new ArrayList<GroupmemberRegular>();
		if(StringUtils.hasText(userIds)){
			
			for(String userStr:userIds.split(",")){
				
				if(!userStr.equals("")){
					userStr=userStr.replace("u", "");
					GroupmemberRegular  gur=new GroupmemberRegular();
					gur.setGroupid(ginfo.getGroupid());
					gur.setUserid(Integer.valueOf(userStr));
					gur.setUpdateTime(new Date());
					gur.setPushflag(0);
					gur.setUpdatetype(1);//1、新增 2、修改 3、删除
					gur.setIsadmin(0);
					groupmemberRegularService.save(gur);
					
					
					//list.add(gur);
				}
				groupmemberRegularService.clear();
				
			}
		}
		
       if(StringUtils.hasText(amdinUserIds)){
			
	        for(String userStr:amdinUserIds.split(",")){
				
				if(!userStr.equals("")){
					userStr=userStr.replace("u", "");
					GroupmemberRegular  gur=new GroupmemberRegular();
					gur.setGroupid(ginfo.getGroupid());
					gur.setUserid(Integer.valueOf(userStr));
					gur.setUpdateTime(new Date());
					gur.setPushflag(0);
					gur.setUpdatetype(1);//1、新增 2、修改 3、删除
					gur.setIsadmin(1);
					groupmemberRegularService.save(gur);
					
					//list.add(gur);
				}
				groupmemberRegularService.clear();
				
			}
		}
       /*if(list.size()>0){
    	   groupmemberRegularService.save(list);
		}*/
        Long modfiytime = updatetime.getTime() / 1000;
        String groupid=ginfo.getGroupid();
        byte[] by = new byte[4+4+1+groupid.length()];
        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
		System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
		byte[] opt = new byte[1];
        opt[0]=1;
		System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
		System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
        groupinfoRegularService.sendMsg(by, 1018);
       
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("添加失败");
		}
		return ajax("添加成功");
	}
	
	public  static synchronized String getmId(){
		
		
		StringBuffer rand=new StringBuffer();
		String  df=new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
		rand.append(df);
		Integer  count=new Random().nextInt(89999)+10000;
		rand.append(count);
		System.out.println(rand.toString());
		return rand.toString();
	}

	
	//添加
	public String addUserInfo(){
		
	try {
			
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String userIds = super.getRequest().getParameter("userIds");
		String groupid = super.getRequest().getParameter("groupid");
		Date updatetime = groupinfoRegularService.getUpdateTime("groupinfo_regular");
		if(StringUtils.hasText(userIds)){
			
			for(String userStr:userIds.split(",")){
				
				if(!userStr.equals("")){
					userStr=userStr.replace("u", "");
					
					DetachedCriteria dt = DetachedCriteria.forClass(GroupmemberRegular.class);	
					 dt.add(Restrictions.eq("groupid",  groupid));
					 dt.add(Restrictions.eq("userid",  Integer.valueOf(userStr)));
					 List lsit=groupinfoRegularService.findByCriteria(dt);
					 if(lsit!=null&&lsit.size()>0){
						 
						 GroupmemberRegular groupmemberRegular= (GroupmemberRegular)lsit.get(0);
						 if(groupmemberRegular.getUpdatetype()==3){
							 groupmemberRegular.setUpdatetype(2);//1、新增 2、修改 3、删除
							 groupmemberRegularService.update(groupmemberRegular);
						 }
						 
					 }else{
					
					GroupmemberRegular  gur=new GroupmemberRegular();
					gur.setGroupid(groupid);
					gur.setUserid(Integer.valueOf(userStr));
					gur.setUpdateTime(new Date());
					gur.setPushflag(0);
					gur.setUpdatetype(1);//1、新增 2、修改 3、删除
					gur.setIsadmin(0);
					groupmemberRegularService.save(gur);
					//list.add(gur);
					 }
				}
				
			}

			UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
			
			
			 DetachedCriteria dt = DetachedCriteria.forClass(GroupmemberRegular.class);	
			 dt.add(Restrictions.eq("groupid",  groupid));
			 Integer count=groupinfoRegularService.findByCriteria(dt).size();
			 DetachedCriteria dt1 = DetachedCriteria.forClass(GroupinfoRegular.class);	
			 dt1.add(Restrictions.eq("groupid", groupid));
			 List list=groupinfoRegularService.findByCriteria(dt1);
			 
			 if(list.size()>0){
				 GroupinfoRegular groupinforegular= (GroupinfoRegular)list.get(0);
				 groupinforegular.setUsernum(Long.valueOf(count));
				 groupinfoRegularService.update(groupinforegular);
				 
				 
				 Long modfiytime = updatetime.getTime() / 1000;
		        byte[] by = new byte[4+4+1+groupid.length()];
		        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
				System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
				byte[] opt = new byte[1];
		        opt[0]=2;
				System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
				System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
		        groupinfoRegularService.sendMsg(by, 1018);
			       
				 
			 }
			 
		}

       
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("添加失败");
		}
		return ajax("true");
	}
	
	
	/****
	 * 修改状态
	 */
	public String  updateState() {
		
		try {

			UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
			
			 DetachedCriteria dt = DetachedCriteria.forClass(GroupinfoRegular.class);	
			 dt.add(Restrictions.eq("groupid",  id.toString()));
			 List lsit=groupinfoRegularService.findByCriteria(dt);
			
			if(lsit!=null&&lsit.size()>0){
				 Date updatetime = groupinfoRegularService.getUpdateTime("groupinfo_regular");
				GroupinfoRegular groupinfoRegular=(GroupinfoRegular)lsit.get(0);
			
				if(utype==0){
					groupinfoRegular.setUpdateTime(new Date());
					groupinfoRegular.setUpdatetype(2);//1、新增 2、修改 3、删除
					groupinfoRegular.setForbidden(1);
					groupinfoRegularService.update(groupinfoRegular);
					
				}else if(utype==1){
					groupinfoRegular.setUpdateTime(new Date());
					groupinfoRegular.setUpdatetype(2);//1、新增 2、修改 3、删除
					groupinfoRegular.setForbidden(0);
					groupinfoRegularService.update(groupinfoRegular);
					
				}
				Long modfiytime = updatetime.getTime() / 1000;
				 String groupid=groupinfoRegular.getGroupid();
				 byte[] by = new byte[4+4+1+groupid.length()];
			        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
					System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
					byte[] opt = new byte[1];
			        opt[0]=2;
					System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
					System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
			        groupinfoRegularService.sendMsg(by, 1018);
			        
			        
			        if(utype==0){
						
						return ajax("注销成功!");
					}else if(utype==1){
						return ajax("启用成功!");
					}
			    //groupinfoRegularService.connectInterfce("groupinfo_regular", 1018,modfiytime.intValue());
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	/****
	 * 修改状态
	 */
	public String  updateAdmin() {
		
		try {
			String userid = super.getRequest().getParameter("userid");
			if(StringUtils.hasText(userid)&&StringUtils.hasText(groupid)){
			DetachedCriteria dt = DetachedCriteria.forClass(GroupmemberRegular.class);	
			 dt.add(Restrictions.eq("groupid",  groupid));
			 dt.add(Restrictions.eq("userid",  Integer.valueOf(userid)));
			 List lsit=groupinfoRegularService.findByCriteria(dt);
			
			if(lsit!=null&&lsit.size()>0){
				Date updatetime = groupinfoRegularService.getUpdateTime("groupmember_regular");
				GroupmemberRegular groupinfoRegular=(GroupmemberRegular)lsit.get(0);
				if(utype==1){
					groupinfoRegular.setUpdateTime(new Date());
					groupinfoRegular.setUpdatetype(2);//1、新增 2、修改 3、删除
					groupinfoRegular.setIsadmin(0);
					groupmemberRegularService.update(groupinfoRegular);
				
				}else if(utype==0){
					groupinfoRegular.setUpdateTime(new Date());
					groupinfoRegular.setUpdatetype(2);//1、新增 2、修改 3、删除
					groupinfoRegular.setIsadmin(1);
					groupmemberRegularService.update(groupinfoRegular);
					
				}
				Long modfiytime = updatetime.getTime() / 1000;
				UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
				 byte[] by = new byte[4+4+1+groupid.length()];
			        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
					System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
					byte[] opt = new byte[1];
			        opt[0]=2;
					System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
					System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
			        groupinfoRegularService.sendMsg(by, 1018);
			    //groupinfoRegularService.connectInterfce("groupmember_regular", 1018,modfiytime.intValue());
			        if(utype==1){
						
						return ajax("注销成功!");
					}else if(utype==0){
						return ajax("启用成功!");
					}
			}
			
			
			
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	
	/****
	 * 删除
	 */
	public String  delete() {
		
		try {
			String userid = super.getRequest().getParameter("userid");
			DetachedCriteria dt = DetachedCriteria.forClass(GroupmemberRegular.class);	
			 dt.add(Restrictions.eq("groupid",  groupid));
			 dt.add(Restrictions.eq("userid",  Integer.valueOf(userid)));
			 List lsit=groupinfoRegularService.findByCriteria(dt);
			
			if(lsit!=null&&lsit.size()>0){
				Date updatetime = groupinfoRegularService.getUpdateTime("groupmember_regular");
				GroupmemberRegular groupinfoRegular=(GroupmemberRegular)lsit.get(0);
				groupinfoRegular.setUpdateTime(new Date());
				groupinfoRegular.setUpdatetype(3);//1、新增 2、修改 3、删除
				
				groupmemberRegularService.update(groupinfoRegular);
				Long modfiytime = updatetime.getTime() / 1000;
				UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);

				 byte[] by = new byte[4+4+1+groupid.length()];
			        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
					System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
					byte[] opt = new byte[1];
			        opt[0]=2;
					System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
					System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
			        groupinfoRegularService.sendMsg(by, 1018);
				//groupinfoRegularService.connectInterfce("groupmember_regular", 1018,modfiytime.intValue());  
				return ajax("删除成功!");
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	/****
	 * 删除
	 */
	public String  deleteALL() {
		
		try {
			
			
			String userIds = super.getRequest().getParameter("userIds");
			String groupid = super.getRequest().getParameter("groupid");
		
			if(StringUtils.hasText(userIds)){
				Date updatetime = groupinfoRegularService.getUpdateTime("groupmember_regular");
				
				for(String userStr:userIds.split(",")){
					
					DetachedCriteria dt = DetachedCriteria.forClass(GroupmemberRegular.class);	
					 dt.add(Restrictions.eq("groupid",  groupid));
					 dt.add(Restrictions.eq("userid",  Integer.valueOf(userStr)));
					 List lsit=groupinfoRegularService.findByCriteria(dt);
					
					if(lsit!=null&&lsit.size()>0){
						GroupmemberRegular groupinfoRegular=(GroupmemberRegular)lsit.get(0);
						groupinfoRegular.setUpdateTime(new Date());
						groupinfoRegular.setUpdatetype(3);//1、新增 2、修改 3、删除
						
						groupmemberRegularService.update(groupinfoRegular);
						Long modfiytime = updatetime.getTime() / 1000;
						UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);

						 byte[] by = new byte[4+4+1+groupid.length()];
					        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
							System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
							byte[] opt = new byte[1];
					        opt[0]=2;
							System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
							System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
					        groupinfoRegularService.sendMsg(by, 1018);
						return ajax("删除成功!");
					}
					
				}
				
			}
			
		
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	/****
	 * 设置常用联系人
	 * @return
	 */
    public String updateLxr(){
    	
    	
    	try {
    		 szUrDate();
    		 DetachedCriteria dt = DetachedCriteria.forClass(UserRoamingdata.class);	
			 dt.add(Restrictions.eq("userid",0));
			 List<UserRoamingdata> userlist=compdeptService.findByCriteria(dt);
			if(userlist!=null&&userlist.size()>0){
				
				
				UserRoamingdata userRoamingdata=userlist.get(0);
			
				
				ServletContext sc = ServletActionContext.getServletContext();
				HashMap userCodeMap=(HashMap)sc.getAttribute("userCodeMap");
					
				if(id!=null){
					Date updatetime = groupinfoRegularService.getUpdateTime("groupinfo_regular");
					Object  obj=userCodeMap.get("U"+id);
					if(obj!=null){
						userCodeMap.remove("U"+id);
					}else{
					    userCodeMap.put("U"+id, id);
					}
					//更新数据
					String str = "";
					if(userCodeMap.size()>0){
					Iterator iterator = userCodeMap.keySet().iterator();
					int i=0;
					while(iterator.hasNext()){
						i++;
						Object key = iterator.next();
						Object val = userCodeMap.get(key);
						if(i==userCodeMap.size()){
						str += val+";";
						}else{
							str += val+"|";
						}
					}	
					}
					userRoamingdata.setContent(str);
					userRoamingdataService.update(userRoamingdata);
					//Long modfiytime = updatetime.getTime() / 1000;
					//groupinfoRegularService.connectInterfce("groupinfo_regular", 1018,modfiytime.intValue());  
				
				}
				
				return ajax("设置成功!");
			}
    		
    		
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    	return null;
    	
    }
    
    
    public void szUrDate(){
    	
   /* 	ServletContext sc = ServletActionContext.getServletContext();
    	if(sc.getAttribute("userCodeMap")==null)*/
		HashMap userCodeMap=new HashMap();
		 DetachedCriteria dt = DetachedCriteria.forClass(UserRoamingdata.class);	
		 dt.add(Restrictions.eq("userid",0));
		 List<UserRoamingdata> userlist=compdeptService.findByCriteria(dt);
		if(userlist!=null&&userlist.size()>0){
			
			UserRoamingdata userRoamingdata=userlist.get(0);
			String ur=userRoamingdata.getContent();
			ur=ur.replaceAll(";", "");
			if(StringUtils.hasText(ur)){
			String[] userRoam= ur.split("\\|");
			for (int i = 0; i < userRoam.length; i++) {
				String str = userRoam[i];
				userCodeMap.put("U"+str, str);
			}
			}
		 ServletContext sc = ServletActionContext.getServletContext();
		 java.util.HashMap verifyCodeMap=(sc.getAttribute("verifyCodeMap")==null?new HashMap():(HashMap)sc.getAttribute("verifyCodeMap"));	
		 sc.setAttribute("userCodeMap", userCodeMap);	
		 
		}else{
			
			UserRoamingdata userRoamingdata=new UserRoamingdata();
			userRoamingdata.setUserid(0);
			userRoamingdata.setContenttype("1");//'漫游数据类型 1：常用联系人，2：常用部门，3：关注人',
			Long time=System.currentTimeMillis();
			userRoamingdata.setUpdatetime(0L);
			userRoamingdata.setContent(";");
			this.userRoamingdataService.save(userRoamingdata);
			 ServletContext sc = ServletActionContext.getServletContext();
			 java.util.HashMap verifyCodeMap=(sc.getAttribute("verifyCodeMap")==null?new HashMap():(HashMap)sc.getAttribute("verifyCodeMap"));	
		}
    	
			
		
    	
    }
    
    
    /****
	 * 修改
	 */
	public String update() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		
		String groupname = super.getRequest().getParameter("groupname");
		String description = super.getRequest().getParameter("description");
		
		 DetachedCriteria dt = DetachedCriteria.forClass(GroupinfoRegular.class);	
		 dt.add(Restrictions.eq("groupid",  groupid));
		 List list=groupinfoRegularService.findByCriteria(dt);
		 if(list!=null&&list.size()>0){
			   Date updatetime = groupinfoRegularService.getUpdateTime("groupmember_regular");
			   GroupinfoRegular mwl= (GroupinfoRegular)list.get(0);
			    mwl.setDescription(description);
			    mwl.setGroupname(groupname);
			    mwl.setUpdatetype(2);////更新标示 1新增  2修改  3删除
			    mwl.setUpdateTime(new Date());
				groupinfoRegularService.update(mwl);
				Long modfiytime = updatetime.getTime() / 1000;
				UserInfo	 userinfo=(UserInfo)super.getRequest().getSession().getAttribute(SystemUtil.USERINFO);
				 byte[] by = new byte[4+4+1+groupid.length()];
			      byte[] opt = new byte[1];
			        opt[0]=2;
			        System.arraycopy(Formatu.int2byte(userinfo.getId().intValue()), 0, by,0, 4); /*操作用户 */
					System.arraycopy(Formatu.int2byte(modfiytime.intValue()), 0, by,4, 4); /*操作时间  */
					System.arraycopy(opt, 0, by,8, 1); /*操作类型 */
					System.arraycopy(groupid.getBytes(), 0, by, 9, groupid.length()); /* 组id */
			        groupinfoRegularService.sendMsg(by, 1018);
				//groupinfoRegularService.connectInterfce("groupinfo_regular", 1018,modfiytime.intValue());  
			
			 response.getWriter().write("true");
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	


	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}



	public int getPageNum() {
		return pageNum;
	}



	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}



	public String getNode() {
		return node;
	}



	public void setNode(String node) {
		this.node = node;
	}





	public Integer getPid() {
		return pid;
	}



	public void setPid(Integer pid) {
		this.pid = pid;
	}



	public CompdeptService getCompdeptService() {
		return compdeptService;
	}



	public void setCompdeptService(CompdeptService compdeptService) {
		this.compdeptService = compdeptService;
	}



	public Integer getDeptid() {
		return deptid;
	}



	public void setDeptid(Integer deptid) {
		this.deptid = deptid;
	}



	public String getUname() {
		return uname;
	}



	public void setUname(String uname) {
		this.uname = uname;
	}



	public Integer getUtype() {
		return utype;
	}



	public void setUtype(Integer utype) {
		this.utype = utype;
	}



	public List getDeptList() {
		return deptList;
	}



	public void setDeptList(List deptList) {
		this.deptList = deptList;
	}



	public String getDeptids() {
		return deptids;
	}



	public void setDeptids(String deptids) {
		this.deptids = deptids;
	}



	public GroupinfoRegular getGroupinfoRegular() {
		return groupinfoRegular;
	}



	public void setGroupinfoRegular(GroupinfoRegular groupinfoRegular) {
		this.groupinfoRegular = groupinfoRegular;
	}



	public String getData() {
		return data;
	}



	public void setData(String data) {
		this.data = data;
	}
	
	
	

	
}
